<!--
 * @Author: tansq
 * @Date: 2021-09-17 15:20:31
 * @LastEditTime: 2022-12-01 14:24:16
 * @LastEditors: tansq
 * @Description: 
 * @FilePath: \hte-vue-dev\src\views\library\company\components\ImportInitData.vue
 * 可以输入预定的版权声明、个性签名、空行等
-->

<template>
  
      
      <div class="content">
        <!-- <a-form-model
          class="hte-form-model"
          ref="ruleForm"
          :model="form"
          :rules="rules"
        >
           <section>
            <div class="section-header">
              公司库初始化数据
             
            </div>
             <div class="hte-btn-group" style="16px 0"> 
                <a-button class="hte-btn add-btn" type="primary" @click="importExcelDataBtn">导入</a-button>
                <a-button class="hte-btn add-btn" @click="handleDelete">删除</a-button>
             
            </div>
         
            <div class="section-table">
              <a-table
                :scroll="{ x: '100%' }"
                class="hte-table-content"
                :columns="payColumns"
                :data-source="sourceData"
                :pagination="pagination"
                rowKey="rowKey"
                :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
                :loading="tabLoading"
                bordered
                size="middle"
                @change="pageChange"
              >
                <template slot="num" slot-scope="text, record, index">
                  {{ (currPageIndex - 1) * currPageSize + parseInt(index) + 1 }}
                </template>
                <template slot="teamName" slot-scope="text, record">
                  <a-input v-model="record.teamName" />
                </template>
                <template slot="completedTotal" slot-scope="text, record">
                  <a-input v-model="record.completedTotal" />
                </template>
                <template slot="operation" slot-scope="text, record">
                  <a @click="handleDelete(record, 'sourceData')">删除</a>
                </template>
              </a-table>
            </div>
          </section>
        
        </a-form-model>         -->
        <excel-paste :downloadKey="4"  :columnsExcel="payColumns" :exportXlsTemplateUrl="importExcle.exportXlsTemplateUrl"  :excelVisible="importExcle.excelVisible" :excelConfirmLoading="importExcle.excelConfirmLoading"   @handleOkExcel="handleOkExcel" @handleCancelExcel="handleCancelExcel"></excel-paste>
        <AreaSelect :display="false" ref="areaSelect"></AreaSelect>
      </div>      
    
</template>
<script>
import '@/config/upload.install.js'
import LibFileAttach from '@/views/library/batchFile/LibFileAttach';
import { companyMixin,companyValidate } from '@/views/library/company/companyMixin'
import {randomUUID} from '@/utils/util'
import { isDateYYYYMMDD,isNumber,isArea } from '@/utils/validate'
import { deleteAction, getAction,postAction,downFile,getFileAccessHttpUrl } from '@/api/manage'
export default {
  name:'ImportInitData',
  components: {
    ExcelPaste:()=>import('@/components/excelPaste/ExcelPaste'),
     AreaSelect:()=>import('@/components/tools/AreaSelect') 
  },
  mixins: [],
  props: {
    dics:{
      type:Object,
      default:()=>{return {}}
    }
  },
  data() {
    return {

      labelCol: { span: 8 },
      wrapperCol: { span: 15 },
      form: {
      },
      pagination: {
        total: 0,
        current: 1,
        pageSize:100
      },
      tabLoading: false,
      currPageIndex: 1,
      currPageSize: 10,
      payColumns: [
        {
          title: '序号',
          dataIndex: 'rowIndex',
          width: '50px',
          align: 'center',
          fixed: 'left',
            customRender:function (t,r,index) {
                return parseInt(index)+1;
            },
            isImportExcelColumns:false
        },
        {
          title: '公司名称',
          fixed: 'left',
          dataIndex: 'name',
          width: '180px',
          align: 'center',
        },
     
         {
          title: '企业资质（多个用逗号“,”隔开）',
          dataIndex: 'qualificationName',
          align: 'center',
          width: '150px',
          ellipsis: true,
        },
        {
          title: '政策管理员',
          dataIndex: 'policyAdmin',
          align: 'center',
          width: '120px',
        },
        {
          title: '政策管理主管（填写账号多个用逗号“,”隔开）',
          dataIndex: 'policyCharge',
          align: 'center',
          width: '120px',
        },
         {
          title: '地址-省市区',
          dataIndex: 'addressAreaLabel',
          align: 'center',
          width: '150px',
        },
          {
          title: '详情地址',
          dataIndex: 'addressDetails',
          align: 'center',
          width: '150px',
          ellipsis: true,
        },
        // {
        //   title: '公司性质',
        //   dataIndex: 'natureOfCompanyName',
        //   align: 'center',
        //   width: '120px',
        // },
      ],
      sourceData: [
       
      ],
      importExcle:{
        excelVisible:false,
        excelConfirmLoading:false,
        exportXlsTemplateUrl:'templates/excel/公司库基础信息模板.xlsx'
      },
      visibleModel: false,
     
      selectedRowKeys:[],
       url:{
        'addBatch':'/libraryCompany/hteLibCompany/importInitData'
      },
    }
  },
  created() {

  },
  mounted() {},
  methods: {
    pageChange(page) {
      this.currPageIndex = page.current
      this.currPageSize = page.pageSize
      this.pagination.current = page.current
      
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
  
    handleClassOk() {
      this.visibleModel = false
    },
    handlePersonnel(type, item) {
      if (type === 'edit') {
        Object.assign(this.visibleForm, item)
      }
      this.visibleModel = true
    },
    handleSelect(){

    },
      handleDelete(obj, list) {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！');
        return;
      } else {
      
        var that = this;
        this.$confirm({
          title: "确认删除",
          content: "是否删除选中数据?",
          onOk: function () {
              
              for (var a = 0; a < that.selectedRowKeys.length; a++) {
               let rowKey = that.selectedRowKeys[a]  ;
               
               that.sourceData.splice( that.sourceData.findIndex(item=>{return item.rowKey==rowKey}),1)
              }

             that.selectedRowKeys = [];
           
          }
        });
      }
    },
     handleOkExcel:function(data = []){
        let that = this;

         this.importExcle.excelConfirmLoading = true;
        //数据验证
        for (let index = 0; index < data.length; index++) {
            const element = data[index];
             let companyNameMore  = this.sourceData.find(item=>{return item.name == element.name }) 
            if(!!companyNameMore){
                this.$message.warning(`操作异常:${companyNameMore.name}公司重复`);
                 this.importExcle.excelConfirmLoading = false;
                return false;
            }
            // if(!companyValidate.checkCompanyData(this,element)){
            //   this.importExcle.excelConfirmLoading = false;
            //   return false;    
            // }         
            element.rowKey = randomUUID() + new Date().getTime();
            if(!isArea(element.addressAreaLabel)){
              this.$message.warning(`操作异常:${element.name}请导入地区格式如下{中国/广东省}！`);
              return false; 
            }
          if(!that.$refs.areaSelect.checkDataByLabel(element.addressAreaLabel)) return false;
            element.addressArea = that.$refs.areaSelect.getValueOrLabel(element.addressAreaLabel,'value')
          }

        postAction(this.url.addBatch,data).then(res=>{
          if(res.code == 200){
            this.$message.success('导入成功');    
            this.handleCancelExcel();
          }else{
            this.$message.error(res.message);
          }
        }).finally(res=>{
           this.importExcle.excelConfirmLoading = false;

        })
        
        that.sourceData.push(...data)
       
        
       
       
      },
     
      importExcelDataBtn:function(){
         this.importExcle.excelConfirmLoading = false;
         this.importExcle.excelVisible = true;
      },
      handleCancelExcel:function(){
         this.$emit("ok")
        this.importExcle.excelVisible = false;
      },
  },

}
</script>
<style lang="less" scoped>
@import '~@assets/less/hte-main.less';
@import '~@views/library/company/company.less';
</style>
